#pragma once
#include "stdafx.h"
#include "RotButton.h"
#include "LabelAni.h"
#include "ButtonAni.h"

class CMonitorWndUI : public CContainerUI
{
	DECLARE_DUICONTROL(CMonitorWndUI)
public:
	CMonitorWndUI()
	{
		CDialogBuilder builder;
		CContainerUI *pContainer = static_cast<CContainerUI *>(builder.Create(_T("monitor.xml"), (UINT)0));
		if (pContainer)
		{
			this->Add(pContainer);
		}
		else
		{
			this->RemoveAll();
			return;
		}
	}

	//virtual CControlUI *CreateControl(LPCTSTR pstrClass)
	//{
	//	if (_tcscmp(pstrClass, _T("RotButton")) == 0)
	//	{
	//		return new CRotButtonUI;
	//	}
	//	else if (_tcscmp(pstrClass, _T("LabelAni")) == 0)
	//	{
	//		return new CLabelAniUI;
	//	}
	//	else if (_tcscmp(pstrClass, _T("ButtonAni")) == 0)
	//	{
	//		return new CButtonAniUI;
	//	}

	//	return NULL;
	//}
};

class MonitorFramWnd
{
public:
	MonitorFramWnd(CPaintManagerUI *pm)
	{
		m_pm = pm;
	}

	void Init()
	{
		m_LabelDNIV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelDNIV")));
		m_LabelHolistatS = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelHolistatS")));
		m_LabelAuxHeatS = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelAuxHeatS")));
		m_LabelHTempV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelHTempV")));
		m_LabelLTempV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelLTempV")));
		m_LabelFlowV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelFlowV")));
		m_LabelHotTankV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelHotTankV")));
		m_LabelColdTankV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelColdTankV")));
		m_LabelCloudV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelCloudV")));
		m_LabelWaterFlowV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelWaterFlowV")));
		m_LabelCondenserFlow = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelCondenserFlow")));
		m_LabelDynamoS = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelDynamoS")));
		m_LabelFluxV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelFluxV")));
		m_LabelReceiverV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelReceiverV")));
		m_LabelReceiveredV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelReceiveredV")));
		m_LabelHotTankInV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelHotTankInV")));
		m_LabelHotTankOutV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelHotTankOutV")));
		m_LabelDynamoOutV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelDynamoOutV")));
		m_LabelSeltUseElecV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelSeltUseElecV")));
		m_LabelNetOutElecV = static_cast<CLabelAniUI *>(m_pm->FindControl(_T("LabelNetOutElecV")));

		m_RotHotSaultFan = static_cast<CRotButtonUI *>(m_pm->FindControl(_T("RotHotSaultFan")));
		m_RotColdSaultFan = static_cast<CRotButtonUI *>(m_pm->FindControl(_T("RotColdSaultFan")));
		m_RotWaterFan1 = static_cast<CRotButtonUI *>(m_pm->FindControl(_T("RotWaterFan1")));
		m_RotWaterFan2 = static_cast<CRotButtonUI *>(m_pm->FindControl(_T("RotWaterFan2")));

		m_to_open_field = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_open_field")));
		m_to_prepare = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_prepare")));
		m_to_aux_heat = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_aux_heat")));
		m_to_open_cold_sault_valve = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_open_cold_sault_valve")));
		m_to_solar_trace = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_solar_trace")));
		m_to_open_hot_sault_valve = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_open_hot_sault_valve")));
		m_to_open_hot_sault_pump = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_open_hot_sault_pump")));
		m_to_close_cold_sault_pump = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_close_cold_sault_pump")));
		m_to_close_holistat = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_close_holistat")));
		m_to_close_hot_sault_valve = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_close_hot_sault_valve")));
		m_to_close_cold_sault_valve = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_close_cold_sault_valve")));
		m_to_close_field = static_cast<CButtonAniUI *>(m_pm->FindControl(_T("to_close_field")));
	}

	BOOL OnLClick(CControlUI *pControl)
	{
		BOOL bOK = FALSE;

		CDuiString sName = pControl->GetName();
		if (sName.CompareNoCase(_T("to_open_field")) == 0)
		{
			to_open_field();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_prepare")) == 0)
		{
			to_prepare();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_aux_heat")) == 0)
		{
			to_aux_heat();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_open_cold_sault_valve")) == 0)
		{
			to_open_cold_sault_valve();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_solar_trace")) == 0)
		{
			to_solar_trace();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_open_hot_sault_valve")) == 0)
		{
			to_open_hot_sault_valve();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_open_hot_sault_pump")) == 0)
		{
			to_open_hot_sault_pump();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_close_cold_sault_pump")) == 0)
		{
			to_close_cold_sault_pump();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_close_holistat")) == 0)
		{
			to_close_holistat();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_close_hot_sault_valve")) == 0)
		{
			to_close_hot_sault_valve();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_close_cold_sault_valve")) == 0)
		{
			to_close_cold_sault_valve();
			bOK = TRUE;
		}
		else if (sName.CompareNoCase(_T("to_close_field")) == 0)
		{
			to_close_field();
			bOK = TRUE;
		}

		return bOK;
	}

private:
	void to_open_field()
	{
		AutoDemonPlay();
		//m_LabelDNIV->ToPlayPeriod();
		//m_LabelCloudV->ToPlayPeriod();
	}
	void to_prepare()
	{
	}
	void to_aux_heat()
	{
	}
	void to_open_cold_sault_valve()
	{
	}
	void to_solar_trace()
	{
	}
	void to_open_hot_sault_valve()
	{
	}
	void to_open_hot_sault_pump()
	{
	}
	void to_close_cold_sault_pump()
	{
	}
	void to_close_holistat()
	{
	}
	void to_close_hot_sault_valve()
	{
	}
	void to_close_cold_sault_valve()
	{
	}
	void to_close_field()
	{
	}

	/// animation chain:
	void AutoDemonInit()
	{
		vecAutoDemon.clear();

		m_to_open_field->SetNextAnimPlayer(m_LabelDNIV);
		m_to_open_field->SetNextAnimPlayer(m_LabelCloudV);
		vecAutoDemon.push_back(m_to_open_field);

		m_to_prepare->SetNextAnimPlayer(m_LabelAuxHeatS);
		m_to_prepare->SetNextAnimPlayer(m_LabelSeltUseElecV);
		m_to_prepare->SetNextAnimPlayer(m_LabelHolistatS);
		vecAutoDemon.push_back(m_to_prepare);

		m_to_aux_heat->SetNextAnimPlayer(m_LabelFlowV);
		m_to_aux_heat->SetNextAnimPlayer(m_LabelHTempV);
		m_to_aux_heat->SetNextAnimPlayer(m_LabelLTempV);
		m_to_aux_heat->SetNextAnimPlayer(m_LabelColdTankV);
		m_to_aux_heat->SetNextAnimPlayer(m_RotColdSaultFan);
		vecAutoDemon.push_back(m_to_aux_heat);

		m_to_open_cold_sault_valve->SetNextAnimPlayer(m_LabelFluxV);
		m_to_open_cold_sault_valve->SetNextAnimPlayer(m_LabelReceiverV);
		m_to_open_cold_sault_valve->SetNextAnimPlayer(m_LabelReceiveredV);
		m_to_open_cold_sault_valve->SetNextAnimPlayer(m_LabelHotTankInV);
		m_to_open_cold_sault_valve->SetNextAnimPlayer(m_LabelHotTankOutV);
		vecAutoDemon.push_back(m_to_open_cold_sault_valve);

		m_to_solar_trace->SetNextAnimPlayer(m_LabelDynamoOutV);
		m_to_solar_trace->SetNextAnimPlayer(m_LabelNetOutElecV);
		m_to_solar_trace->SetNextAnimPlayer(m_RotHotSaultFan);
		m_to_solar_trace->SetNextAnimPlayer(m_LabelHotTankV);
		m_to_solar_trace->SetNextStopPlayer(m_LabelAuxHeatS);
		m_to_solar_trace->SetNextAnimPlayer(m_RotWaterFan1);
		m_to_solar_trace->SetNextAnimPlayer(m_RotWaterFan2);
		m_to_solar_trace->SetNextAnimPlayer(m_LabelWaterFlowV);
		m_to_solar_trace->SetNextAnimPlayer(m_LabelCondenserFlow);
		m_to_solar_trace->SetNextAnimPlayer(m_LabelDynamoS);
		vecAutoDemon.push_back(m_to_solar_trace);

		vecAutoDemon.push_back(m_to_open_hot_sault_valve);

		m_to_open_hot_sault_pump->SetNextStopPlayer(m_RotColdSaultFan);
		vecAutoDemon.push_back(m_to_open_hot_sault_pump);

		m_to_close_cold_sault_pump->SetNextStopPlayer(m_LabelFlowV);
		m_to_close_cold_sault_pump->SetNextStopPlayer(m_LabelHTempV);
		m_to_close_cold_sault_pump->SetNextStopPlayer(m_LabelLTempV);
		m_to_close_cold_sault_pump->SetNextStopPlayer(m_LabelColdTankV);
		m_to_close_cold_sault_pump->SetNextStopPlayer(m_LabelHolistatS);
		vecAutoDemon.push_back(m_to_close_cold_sault_pump);

		m_to_close_holistat->SetNextStopPlayer(m_RotHotSaultFan);
		vecAutoDemon.push_back(m_to_close_holistat);

		m_to_close_hot_sault_valve->SetNextStopPlayer(m_RotHotSaultFan);
		vecAutoDemon.push_back(m_to_close_hot_sault_valve);

		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelHotTankInV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelHotTankOutV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_RotWaterFan1);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_RotWaterFan2);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelDynamoOutV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelFluxV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelReceiverV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelReceiveredV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelSeltUseElecV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelNetOutElecV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelHotTankV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelWaterFlowV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelCondenserFlow);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelDynamoS);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelDNIV);
		m_to_close_cold_sault_valve->SetNextStopPlayer(m_LabelCloudV);
		vecAutoDemon.push_back(m_to_close_cold_sault_valve);

		vecAutoDemon.push_back(m_to_close_field);
	}

	void AutoDemonFlow()
	{
		/// to init flow vector
		SolarThermalShow *next = NULL;
		for (vector<SolarThermalShow *>::iterator iter = vecAutoDemon.begin(); iter != vecAutoDemon.end(); iter++)
		{
			if (iter + 1 != vecAutoDemon.end())
			{
				next = *(iter + 1);
				if (next)
				{
					(*iter)->SetNextAnimPlayer(next);
				}
			}
		}
	}

	void AutoDemonPlay()
	{

		AutoDemonReset();
		AutoDemonInit();
		AutoDemonFlow();

		if (vecAutoDemon.size() == 0)
		{
			return;
		}
		(*vecAutoDemon.begin())->ToActFlowAnim();
	}

	void AutoDemonReset()
	{
		if (vecAutoDemon.size() == 0)
		{
			return;
		}

		for (vector<SolarThermalShow *>::iterator iter = vecAutoDemon.begin(); iter != vecAutoDemon.end(); iter++)
		{
			(*iter)->ToResetFlowAnim();
		}
	}

private:
	CPaintManagerUI *m_pm;

	/// ****************************************
	CLabelAniUI *m_LabelDNIV;
	CLabelAniUI *m_LabelHolistatS;
	CLabelAniUI *m_LabelAuxHeatS;
	CLabelAniUI *m_LabelHTempV;
	CLabelAniUI *m_LabelLTempV;
	CLabelAniUI *m_LabelFlowV;
	CLabelAniUI *m_LabelHotTankV;
	CLabelAniUI *m_LabelColdTankV;
	CLabelAniUI *m_LabelCloudV;
	CLabelAniUI *m_LabelWaterFlowV;
	CLabelAniUI *m_LabelCondenserFlow;
	CLabelAniUI *m_LabelDynamoS;
	CLabelAniUI *m_LabelFluxV;
	CLabelAniUI *m_LabelReceiverV;
	CLabelAniUI *m_LabelReceiveredV;
	CLabelAniUI *m_LabelHotTankInV;
	CLabelAniUI *m_LabelHotTankOutV;
	CLabelAniUI *m_LabelDynamoOutV;
	CLabelAniUI *m_LabelSeltUseElecV;
	CLabelAniUI *m_LabelNetOutElecV;

	CRotButtonUI *m_RotHotSaultFan;
	CRotButtonUI *m_RotColdSaultFan;
	CRotButtonUI *m_RotWaterFan1;
	CRotButtonUI *m_RotWaterFan2;

	CButtonAniUI *m_to_open_field;
	CButtonAniUI *m_to_prepare;
	CButtonAniUI *m_to_aux_heat;
	CButtonAniUI *m_to_open_cold_sault_valve;
	CButtonAniUI *m_to_solar_trace;
	CButtonAniUI *m_to_open_hot_sault_valve;
	CButtonAniUI *m_to_open_hot_sault_pump;
	CButtonAniUI *m_to_close_cold_sault_pump;
	CButtonAniUI *m_to_close_holistat;
	CButtonAniUI *m_to_close_hot_sault_valve;
	CButtonAniUI *m_to_close_cold_sault_valve;
	CButtonAniUI *m_to_close_field;

	/// ani chain:
	vector<SolarThermalShow *> vecAutoDemon;
};
